<?php

namespace App\Http\Controllers\home;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
//use App\Http\Requests\UserPostRequest;//引入请求控制器
use DB;//引入数据库对象
use Hash;//引入加密算法
use App\Http\Org\Ucpaas;//引入验证码的类

class UserController extends Controller
{
   
   //发送验证码
    public function getYzm(Request $request)
    {
        $phone=$request->input('phone');
        //初始化必填
        $options['accountsid']='1278a551d4ec810c7b0f6c4fb0baec92';
        $options['token']='747303bb1e9bd1cdf5b9d205acdd1f43';

        //初始化 $options必填
        $ucpass = new Ucpaas($options);

        //开发者账号信息查询默认为json或xml

        $ucpass->getDevinfo('json');

        //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
        $appId = "9b7bb682827a4f83a0e412dac327bf67";
        $to = $phone;//接收的手机号
        $templateId = "41724";
        $code = rand(1000,9999);
        $param=$code;
        $ucpass->templateSMS($appId,$to,$templateId,$param);
        //把验证码存入到session中
        session_start();
        $_SESSION['code']=$code;
        echo $_SESSION['code'];
    }
    //验证验证码
    public function getRegister(Request $request){
        //获取session中的验证码
         session_start();
       $code=$_SESSION['code'];

       //获取传过来的验证码
       $yzm=$request->input('code');
        if ($yzm == $code) {
            echo 1;//验证成功
        }else{
            echo 2;//验证失败
        }
    }
    //用户注册
    public function postInsert(Request $request)
    {
        
        //提取数据
        $data = $request->only(['fullname','password1','phone']);
        //进行密码加密
        $password= Hash::make($data['password1']);
        $phone=$data['phone'];
        $name=$data['fullname'];
        //执行数据入库操作
        $res = DB::table('user')->insertGetId(['name'=>$name,'phone'=>$phone,'password'=>$password]);
        //插入数据详情表
        $pic='/uploads/1.jpg';
        if ($res) {
             $res1 = DB::table('userDetails')->insertGetId(['uid'=>$res,'pic'=>$pic,'nickName'=>$name]);
            //遍历userdetail 存session
             $res3= DB::table('userDetails')->where('uid',$res)->get();

            $nname=$res3[0]->nickName;
            $pic=$res3[0]->pic;
            // dd($res);
            //将id,用户名,昵称,图片写入到session中
            session_start();
            $_SESSION['id']=$res;
            // $_SESSION['name']=$name;
            $_SESSION['nname']=$nname;  
            $_SESSION['pic']=$pic;  
            //查询所有的表
            $res2=DB::table('userdetails')->get();
              return view('home.index2',['res2'=>$res2]);
        }
        
    }
    //ajax用户关注
    public function getGzuser(Request $request){
        //获取nid
        $nid=$request->input('uid');
        //通过session获取当前用户的id
        session_start();
        $uid=$_SESSION['id'];
        //把数据存入到关注关系表中
        $res=DB::table('follow')->insertGetId(['uid'=>$uid,'nid'=>$nid]);
        if ($res) {
            echo $res;
        }
    }
    //退出登录
    public function getOutlogin (Request $request){
        session_start();
        session_destroy();
        unset($_SESSION);
        // setcookie(PHPSESSID,'',time()-1,'/');
        return view('home.login.login');
    }
    //编辑用户资料  ajax方法
    public function getUserupdate(Request $request){
        // $id=session('id')
        //接收id和value的值
        $id=$request->input('id');
        $sex=$request->input('attr');
        //执行语句
        if ($sex) {
           $res=DB::table('userDetails')->where('uid',$id)->update(['sex'=>$sex]);
           // echo $res;exit;
        if($res){
            echo 1;
        }else{
            echo 2;
        }
        }
        
        //修改个人简介
        $int=$request->input('val');
        if ($int) {
            $res1=DB::table('userDetails')->where('uid',$id)->update(['introduce'=>$int]);
         if($res1){
            echo 1;
        }else{
            echo 2;
        }

        }
        
        //修改居住地
        //修改个人简介
        $int1=$request->input('val1');
        if ($int1) {
            $res2=DB::table('userDetails')->where('uid',$id)->update(['domicile'=>$int1]);
         if($res2){
            echo 1;
        }else{
            echo 2;
        }
        }

        //修改行业信息
         $int2=$request->input('val2');
        if ($int2) {
            $res2=DB::table('userDetails')->where('uid',$id)->update(['industry'=>$int2]);
         if($res2){
            echo 1;
        }else{
            echo 2;
        }
        }

         //修改签名信息
         $int3=$request->input('val3');
        if ($int3) {
            $res2=DB::table('userDetails')->where('uid',$id)->update(['autograph'=>$int3]);
         if($res2){
            echo 1;
        }else{
            echo 2;
        }
        }
       
    }
    //用户登录处理ajax
    public function getLogin(Request $request)
    {
        //接收用户名
        $name=$request->input('name');
        //接收密码
        $password=$request->input('password');
        //查出数据
        $res=DB::table('user')->where('name',$name)->get();
        if (!$res) {
           echo 1;//用户名不正确
           die;
        }
        //取出密码
        $pwd=$res[0]->password;
       //密码匹对
        $res1=Hash::check($password,$pwd);
        if (!$res1) {
            echo 2;//密码错误
            die;
        }
        if($res && $res1){
            echo 3;//用户名和密码都对
        }
    }
    //处理登录的方法
     public function postLogin(Request $request){
        //接收用户名
        $name=$request->input('name');
        //查出数据
        $res=DB::table('user')->where('name',$name)->get();
        //获取id
        $uid=$res[0]->id;
        //获取用户的权限
        $status=$res[0]->status;
        if($status==1){
            echo "<script>alert('您的账号已被关闭,请联系知乎管理员!');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
            exit;
        }
        if ($res) {    
            //查询用户详情表
            $res1=DB::table('userDetails')->where('uid',$uid)->get();
            $nname=$res1[0]->nickName;
            $pic=$res1[0]->pic;
            //将id,用户名,昵称,图片写入到session中
            session_start();
            $_SESSION['id']=$uid;
            $_SESSION['name']=$name;
            $_SESSION['nname']=$nname;  
            $_SESSION['pic']=$pic;  
        }

        //开始遍历前台首页
        //查询用户关注了谁,并且取出用户的详细信息
        $arr=[];
         $data=DB::table('follow')
            ->join('guanxihd','follow.nid','=','guanxihd.u1id')
            ->where('uid',$uid)
            ->get();
        //查出关注的人的回答
         foreach ($data as $k => $v) {
            //取出答案id
            $id=$v->awid;
            // var_dump($uid);
            // 根据回答关系表查出数据
                 $data1=DB::table('answer')
                ->join('issue','answer.Iid','=','issue.id')
                ->join('userDetails','answer.uid','=','userDetails.uid')
                ->select('answer.*','issue.atitle','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('answer.id',$id)
                ->get();
               
                     //把查到的数据存到数组中             
                    $arr[$k]['ititle']=$data1[0]->atitle;
                    $arr[$k]['content']=$data1[0]->content;
                    $arr[$k]['nickName']=$data1[0]->nickName;
                    $arr[$k]['introduce']=$data1[0]->introduce;
                    $arr[$k]['pic']=$data1[0]->pic;
                    $arr[$k]['time']=$v->time;
                    $arr[$k]['pass']=$data1[0]->pass; 
                    $arr[$k]['id']=$data1[0]->id;//取出答案id
                    $arr[$k]['bs']=0;//此为回答的标识
         }
          // var_dump($arr);
         //查询关注的人的点赞答案
         $data2=DB::table('follow')
            ->join('guanxida','follow.nid','=','guanxida.uid')
            ->join('userDetails','guanxida.uid','=','userDetails.uid')
            ->where('follow.uid',$uid)
            ->get();
               // dd($data2);
            $arr1=[];
         foreach ($data2 as $k => $v) {
             //取出答案id
            $id=$v->did;
             // 根据回答关系表查出数据
                 $data3=DB::table('answer')
                ->join('issue','answer.Iid','=','issue.id')
                ->join('userDetails','answer.uid','=','userDetails.uid')
                ->select('answer.*','issue.atitle','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('answer.id',$id)
                ->get();
                // dd($data3);
                    // var_dump($data3);
                     //把查到的数据存到数组中             
                    $arr1[$k]['ititle']=$data3[0]->atitle;
                    $arr1[$k]['content']=$data3[0]->content;
                    $arr1[$k]['nickName']=$data3[0]->nickName;//发表答案的用户昵称
                    $arr1[$k]['introduce']=$data3[0]->introduce;
                    $arr1[$k]['pic']=$v->pic;
                    $arr1[$k]['time']=$v->datime;//点赞的时间
                    $arr1[$k]['uid']=$v->uid;//获取点赞的人的uid
                    $arr1[$k]['nname']=$v->nickName;//这是谁点的赞用户昵称
                    $arr1[$k]['datime']=$data3[0]->time;//此时间为答案的发表时间
                    $arr1[$k]['pass']=$data3[0]->pass;
                    $arr1[$k]['id']=$data3[0]->id;//取出答案id
                    $arr1[$k]['bs']=1;//此为点赞答案的标识
         }
         // dd($arr1);
         //查询关注的人点赞文章
         $data4=DB::table('follow')
            ->join('guanxiwz','follow.nid','=','guanxiwz.uuuid')
            ->join('userDetails','guanxiwz.uuuid','=','userDetails.uid')
            ->where('follow.uid',$uid)
            ->get();
        $arr2=[];
         foreach ($data4 as $k => $v) {
             //取出文章id
            $id=$v->wzid;
            // dd($v);
             // 根据回答关系表查出数据
                 $data5=DB::table('columnArticle')
                ->join('userDetails','columnArticle.uid','=','userDetails.uid')
                ->select('columnArticle.*','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('columnArticle.id',$id)
                ->get();
                // var_dump($data5);
                     //把查到的数据存到数组中  

                    @$arr2[$k]['ititle']=$data5[0]->ctitle;
                    @$arr2[$k]['content']=$data5[0]->ccontent;
                    @$arr2[$k]['nickName']=$data5[0]->nickName;//发表文章的用户昵称
                    @$arr2[$k]['nname']=$v->nickName;//这是谁点的赞用户昵称
                    @$arr2[$k]['introduce']=$data5[0]->introduce;
                    @$arr2[$k]['pic']=$v->pic;
                    @$arr2[$k]['time']=$v->wztime;//什么时候赞的文章
                    // $arr2[$k]['time']=$data5[0]->time;
                    @$arr2[$k]['pass']=$data5[0]->pass;
                    @$arr2[$k]['id']=$data5[0]->id;//取出文章id
                    @$arr2[$k]['bs']=2;//此为点赞文章的标识
         }
         // die;
          //查询关注的人关注的问题
          $data6=DB::table('follow')
            ->join('guanxiwt','follow.nid','=','guanxiwt.uuid')
            ->join('userDetails','guanxiwt.uuid','=','userDetails.uid')
            ->where('follow.uid',$uid)
            ->get();
            $arr3=[];
            foreach ($data6 as $k => $v) {
                //取出问题的id
                $id=$v->wid;
                // 根据关注问题关系表查出数据
                 $data7=DB::table('issue')
                ->join('userDetails','issue.uid','=','userDetails.uid')
                ->select('issue.*','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('issue.id',$id)
                ->get();
                // var_dump($data7);
                    // 把查到的数据存到数组中             
                    $arr3[$k]['ititle']=$data7[0]->atitle;
                    $arr3[$k]['nickName']=$data7[0]->nickName;
                    $arr3[$k]['introduce']=$data7[0]->introduce;
                    $arr3[$k]['pic']=$v->pic;
                    $arr3[$k]['time']=$v->wtime;
                    $arr3[$k]['nname']=$v->nickName;//这是谁关注的问题
                    $arr3[$k]['id']=$data7[0]->id;//取出问题id
                    $arr3[$k]['bs']=3;//此关注问题的标识
            }
            
             //根据时间把4个数组合并到一个数组中
             $tarr[]=$arr;
             $tarr[]=$arr1;
             $tarr[]=$arr2;
             $tarr[]=$arr3;
            
            //三维数组转换成二维数组
            $array=[];
            foreach ($tarr as $k => $v) {
                foreach($v as $k=>$value){
                    $array[]=$value;
                }
            }
             //按照时间进行排序
            $sort = array(
             'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
             'field'     => 'time',       //排序字段
             );
             $arrSort = array();
             foreach($array AS $uniqid => $row){
                 foreach($row AS $key=>$value){
                     $arrSort[$key][$uniqid] = $value;
                 }
             }
             if($sort['direction']){
                 @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
             }  

              //查询该用户是否有专栏  查询用户专栏
            $resss = DB::table('column')->where('uid',$uid)->get();           
         return view('home.index',['uid'=>$uid,'res'=>$res,'name'=>$name,'nname'=>$nname,'array'=>$array,'resss'=>$resss]);//把用户的id带过去
    }

    //个人资料到首页跳转页面
    public function tzsy(Request $request){
        $uid=$_SESSION['id'];
         //查询用户关注了谁,并且取出用户的详细信息
        $arr=[];
         $data=DB::table('follow')
            ->join('guanxihd','follow.nid','=','guanxihd.u1id')
            ->where('uid',$uid)
            ->get();
        //查出关注的人的回答
         foreach ($data as $k => $v) {
            //取出答案id
            $id=$v->awid;
            // var_dump($uid);
            // 根据回答关系表查出数据
                 $data1=DB::table('answer')
                ->join('issue','answer.Iid','=','issue.id')
                ->join('userDetails','answer.uid','=','userDetails.uid')
                ->select('answer.*','issue.atitle','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('answer.id',$id)
                ->get();
               
                     //把查到的数据存到数组中             
                    $arr[$k]['ititle']=$data1[0]->atitle;
                    $arr[$k]['content']=$data1[0]->content;
                    $arr[$k]['nickName']=$data1[0]->nickName;
                    $arr[$k]['introduce']=$data1[0]->introduce;
                    $arr[$k]['pic']=$data1[0]->pic;
                    $arr[$k]['time']=$v->time;
                    $arr[$k]['pass']=$data1[0]->pass; 
                    $arr[$k]['id']=$data1[0]->id;//取出答案id
                    $arr[$k]['bs']=0;//此为回答的标识
         }
          // var_dump($arr);
         //查询关注的人的点赞答案
         $data2=DB::table('follow')
            ->join('guanxida','follow.nid','=','guanxida.uid')
            ->join('userDetails','guanxida.uid','=','userDetails.uid')
            ->where('follow.uid',$uid)
            ->get();
              // dd($data2);
            $arr1=[];
         foreach ($data2 as $k => $v) {
             //取出答案id
            $id=$v->did;
             // 根据回答关系表查出数据
                 $data3=DB::table('answer')
                ->join('issue','answer.Iid','=','issue.id')
                ->join('userDetails','answer.uid','=','userDetails.uid')
                ->select('answer.*','issue.atitle','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('answer.id',$id)
                ->get();
                    // var_dump($data3);
                     //把查到的数据存到数组中             
                    $arr1[$k]['ititle']=$data3[0]->atitle;
                    $arr1[$k]['content']=$data3[0]->content;
                    $arr1[$k]['nickName']=$data3[0]->nickName;//发表答案的用户昵称
                    $arr1[$k]['introduce']=$data3[0]->introduce;
                    $arr1[$k]['pic']=$v->pic;
                    $arr1[$k]['time']=$v->datime;//点赞的时间
                    $arr1[$k]['uid']=$v->uid;//获取点赞的人的uid
                    $arr1[$k]['nname']=$v->nickName;//这是谁点的赞用户昵称
                    $arr1[$k]['datime']=$data3[0]->time;//此时间为答案的发表时间
                    $arr1[$k]['pass']=$data3[0]->pass;
                    $arr1[$k]['id']=$data3[0]->id;//取出答案id
                    $arr1[$k]['bs']=1;//此为点赞答案的标识
         }
         // dd($arr1);
         //查询关注的人点赞文章
         $data4=DB::table('follow')
            ->join('guanxiwz','follow.nid','=','guanxiwz.uuuid')
            ->join('userDetails','guanxiwz.uuuid','=','userDetails.uid')
            ->where('follow.uid',$uid)
            ->get();
        $arr2=[];
         foreach ($data4 as $k => $v) {
             //取出文章id
            $id=$v->wzid;
            // dd($v);
             // 根据回答关系表查出数据
                 $data5=DB::table('columnArticle')
                ->join('userDetails','columnArticle.uid','=','userDetails.uid')
                ->select('columnArticle.*','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('columnArticle.id',$id)
                ->get();
                // var_dump($data5);
                     //把查到的数据存到数组中             
                    @$arr2[$k]['ititle']=$data5[0]->ctitle;
                    @$arr2[$k]['content']=$data5[0]->ccontent;
                    @$arr2[$k]['nickName']=$data5[0]->nickName;//发表文章的用户昵称
                    @$arr2[$k]['nname']=$v->nickName;//这是谁点的赞用户昵称
                    @$arr2[$k]['introduce']=$data5[0]->introduce;
                    @$arr2[$k]['pic']=$v->pic;
                    @$arr2[$k]['time']=$v->wztime;//什么时候赞的文章
                    // $arr2[$k]['time']=$data5[0]->time;
                    @$arr2[$k]['pass']=$data5[0]->pass;
                    @$arr2[$k]['id']=$data5[0]->id;//取出文章id
                    @$arr2[$k]['bs']=2;//此为点赞文章的标识
         }
         // die;
          //查询关注的人关注的问题
          $data6=DB::table('follow')
            ->join('guanxiwt','follow.nid','=','guanxiwt.uuid')
            ->join('userDetails','guanxiwt.uuid','=','userDetails.uid')
            ->where('follow.uid',$uid)
            ->get();
            $arr3=[];
            foreach ($data6 as $k => $v) {
                //取出问题的id
                $id=$v->wid;
                // 根据关注问题关系表查出数据
                 $data7=DB::table('issue')
                ->join('userDetails','issue.uid','=','userDetails.uid')
                ->select('issue.*','userDetails.introduce','userDetails.nickName','userDetails.pic')
                ->where('issue.id',$id)
                ->get();
                // var_dump($data7);
                    // 把查到的数据存到数组中             
                    $arr3[$k]['ititle']=$data7[0]->atitle;
                    $arr3[$k]['nickName']=$data7[0]->nickName;
                    $arr3[$k]['introduce']=$data7[0]->introduce;
                    $arr3[$k]['pic']=$v->pic;
                    $arr3[$k]['time']=$v->wtime;
                    $arr3[$k]['nname']=$v->nickName;//这是谁关注的问题
                    $arr3[$k]['id']=$data7[0]->id;//取出问题id
                    $arr3[$k]['bs']=3;//此关注问题的标识
            }
            
             //根据时间把4个数组合并到一个数组中
             $tarr[]=$arr;
             $tarr[]=$arr1;
             $tarr[]=$arr2;
             $tarr[]=$arr3;
            
            //三维数组转换成二维数组
            $array=[];
            foreach ($tarr as $k => $v) {
                foreach($v as $k=>$value){
                    $array[]=$value;
                }
            }
             //按照时间进行排序
            $sort = array(
             'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
             'field'     => 'time',       //排序字段
             );
             $arrSort = array();
             foreach($array AS $uniqid => $row){
                 foreach($row AS $key=>$value){
                     $arrSort[$key][$uniqid] = $value;
                 }
             }
             if($sort['direction']){
                 @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
             }
               //查询该用户是否有专栏  查询用户专栏
            $resss = DB::table('column')->where('uid',$uid)->get();               
         return view('home.index',['uid'=>$uid,'array'=>$array,'resss'=>$resss]);
    }
    //查询用户的详细资料的方法
    public function cxzl(Request $request){
        //用户id
        $uid=$request->input('id');
        //开始查询用户数据
        $data=DB::table('userDetails')->where('uid',$uid)->get();
        //查询用户的动态  
        $arr=[];
        //点赞答案关系表
        $da=DB::table('guanxida')->where('uid',$uid)->orderby('datime','desc')->get();
        foreach ($da as $k1 => $v) {
             $arr[$k1]['time']=$v->datime;
            //查询答案 需要的数据压入数组
            $hd=DB::table('answer')->where('id',$v->did)->get();
            foreach ($hd as $k => $v) {
                $arr[$k1]['content']=$v->content;
                $arr[$k1]['pass']=$v->pass;
                 //查询问题表  需要的数据压入数组
                    $wt=DB::table('issue')->where('id',$v->Iid)->get();
                    
                    foreach ($wt as $k2 => $v) {
                        $arr[$k1]['ititle']=$v->atitle;
                        $ud=DB::table('userDetails')->where('uid',$v->uid)->get();
                       foreach ($ud as $k => $v) {
                        //把用户详情数据压入数组
                           $arr[$k1]['upic']=$v->pic;
                           $arr[$k1]['uname']=$v->nickName;
                           $arr[$k1]['yjh']=$v->introduce;
                       }
                        //把需要的数据压入数组中
                    }
            }
           
        }
        //问题关注关系表
        $arr1=[];
        $wt=DB::table('guanxiwt')->where('uuid',$uid)->orderby('wtime','desc')->get();
        foreach ($wt as $k1 => $v) {
            $arr1[$k1]['time']=$v->wtime;
            $wt=DB::table('issue')->where('id',$v->wid)->get();
            //根据关系表查出来问题标题
            foreach ($wt as $k => $v) {
                $arr1[$k1]['atitle']=$v->atitle;
            }
        }
        //赞文章关系表
        $arr2=[];
        $wz=DB::table('guanxiwz')->where('uuuid',$uid)->orderby('wztime','desc')->get();
        foreach ($wz as $k1 => $v) {
            $arr2[$k1]['time']=$v->wztime;
            //查出文章的内容
            $rw=DB::table('columnArticle')->where('id',$v->wzid)->get();
             
            foreach ($rw as $k => $v) {
               //根据文章查出用户信息来
                 $arr2[$k1]['content']=$v->ccontent;
                 $arr2[$k1]['atitle']=$v->ctitle;
                 $arr2[$k1]['pass']=$v->pass;
                $ud=DB::table('userDetails')->where('uid',$v->uid)->get();
                     foreach ($ud as $k => $v) {
                       
                        //把用户详情数据压入数组
                           $arr2[$k1]['upic']=$v->pic;
                           $arr2[$k1]['uname']=$v->nickName;
                           $arr2[$k1]['yjh']=$v->introduce;
                       }
            }
        }
        //根据时间把三个数组合并到一个数组中
        $tarr[]=$arr;
        $tarr[]=$arr1;
        $tarr[]=$arr2;
        //var_dump(array_values($tarr));
        //三维数组转换成二维数组
        $array=[];
        foreach ($tarr as $k => $v) {
            foreach($v as $k=>$value){
                $array[]=$value;
            }
        }
        //按照时间进行排序
        $sort = array(
         'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
         'field'     => 'time',       //排序字段
         );
         $arrSort = array();
         foreach($array AS $uniqid => $row){
             foreach($row AS $key=>$value){
                 $arrSort[$key][$uniqid] = $value;
             }
         }
         if($sort['direction']){
             @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
         }

        //回答模块信息 
        $data1=DB::table('answer')
                ->leftjoin('issue','answer.Iid','=','issue.id')
                ->select('answer.*','issue.atitle')
                ->where('answer.uid',$uid)
                ->orderby('time','desc')
                ->get();
        //统计回答个数
        $hdcount=DB::table('answer')->where('uid',$uid)->count();

        //用户关注的人信息数据
        $data2=DB::table('follow')
                ->leftjoin('userDetails','follow.nid','=','userDetails.uid')
                ->select('userDetails.*')
                ->where('follow.uid',$uid)
                ->get();
        //谁关注了我的数据
         $data3=DB::table('follow')
                ->leftjoin('userDetails','follow.uid','=','userDetails.uid')
                ->select('userDetails.*')
                ->where('follow.nid',$uid)
                ->get();
        //查询用户收藏夹
        $collect=DB::table('userDetails')
            ->join('collect','userDetails.uid','=','collect.uid')
            ->select('userDetails.*','collect.*')
            ->where('userDetails.uid',$uid)
            ->get();

        //查询收藏夹个数
        $sccount = DB::table('collect')
            ->where('collect.uid',$uid)
            ->select('collect.atitle')
            ->count();
        //查询用户问题
        $res = DB::table('issue')
            ->where('issue.uid',$uid)
            ->orderby('time','desc')
            ->get();
        //查询问题个数
        $wtcount = DB::table('issue')
            ->where('issue.uid',$uid)
            ->select('issue.atitle')
            ->count();

        //统计此用户的赞数  答案表
        $num1=DB::table('answer')->select('pass')->where('uid',$uid)->get();
        // var_dump($countgz);
        //遍历数据
        $countz1='';
        foreach ($num1 as $k => $v) {
            //获取赞数这个字段
            $countz=intval($v->pass);

            //赞数相加
            $countz1 += $countz;
        }

        //统计此用户的赞数  文章表
        $num2=DB::table('article')->select('pass')->where('uid',$uid)->get();
        //var_dump($data);
        //遍历数据
        $countz2='';
        foreach ($num2 as $k => $v) {
            //获取赞数这个字段
            $countz=intval($v->pass);

            //赞数相加
            $countz2 += $countz;
        }
         
        //统计此用户的赞数  专栏文章表
        $num3=DB::table('columnArticle')->select('pass')->where('uid',$uid)->get();
        //var_dump($data);
        //遍历数据
        $countz3='';
        foreach ($num3 as $k => $v) {
            //获取赞数这个字段
            $countz=intval($v->pass);

            //赞数相加
            $countz3 += $countz;
        }

        //获取所有的赞数
        $ctzs=$countz3+$countz2+$countz1;

        //查询用户感谢次数
        $thank = DB::table('answer')->where('uid',$uid)->select('answer.thank')->get();
        //通过模板把数据带过去  我的动态 我的回答  我的评论  统计出来的回答个数  用户关注的人 关注我的人 成就系统  我的收藏夹collect  sccount收藏夹个数  问题个数 ctzs总赞数 
        return view('home.user.userdata',['data'=>$data,'array'=>$array,'data1'=>$data1,'hdcount'=>$hdcount,'data2'=>$data2,'data3'=>$data3,'collect'=>$collect,'sccount'=>$sccount,'res'=>$res,'wtcount'=>$wtcount,'ctzs'=>$ctzs,'thank'=>$thank]);
    }

     //执行用户头像上传
    public function postPicup(Request $request)
    {

        //调用方法进行头像上传
        // $data['pic'] = $this->upload($request);
         $id=$request->input('id');
          //提取数据
        $data = $request->except(['_token']);
        //进行入库前的处理
        $pic = self::upload($request);
        //执行数据入库操作
        $res=DB::table('userDetails')->where('id',$id)->update(['pic'=>$pic]);
        //跳转到列表页
        if($res){
          session_start();
          $_SESSION['pic']=$pic;
           echo "<script>alert('上传成功！');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
         }else{
            echo "<script>alert('上传失败!');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
         }
    }
    //处理头像上传
    static public function upload($request)
    {
        //判断是否有文件上传
        if($request->hasFile('pic')){
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取文件的后缀名
            $suffix = $request->file('pic')->getClientOriginalExtension();
            $arr = ['png','jpeg','gif','jpg'];
            if(!in_array($suffix,$arr)){
                return back()->with('error','上传文件格式不正确');
            }
            $request->file('pic')->move('./uploads/', $name.'.'.$suffix);
            //返回路径
            return '/uploads/'.$name.'.'.$suffix;
        }
        }

    //ajax操作点赞 加关注 
    public function getUppass(Request $request){
        //获取发送过来的id  id1点赞回答问题
        $id1=$request->input('id1');
        if ($id1) {
            $pass=$request->input('pass');
            
            $npass=$pass+1;//点赞数加1
            $res=DB::table('answer')->where('id',$id1)->update(['pass'=>$npass]);//进行修改操作
            //用户执行了点赞答案操作,往点赞答案表中插入数据
            //谁点的赞当然是当前用户了 获取当前用户的id
            session_start();
            $uid=$_SESSION['id'];
            // //获取当前的时间
            $datime=time();
            $res1=DB::table('guanxida')->insertGetId(['uid'=>$uid,'did'=>$id1,'datime'=>$datime]);
            if ($res && $res1) {
               echo $npass;
            }else{
                echo 0;
            }
    }
    //ajax取消点赞操作 

         //获取发送过来的id  id2点赞文章
        $id2=$request->input('id2');
        if ($id2) {
            $pass=$request->input('pass');
            
            $npass=$pass+1;//点赞数加1
            $res=DB::table('columnArticle')->where('id',$id2)->update(['pass'=>$npass]);//进行修改操作
            //用户执行了点赞答案操作,往点赞答案表中插入数据
            //谁点的赞当然是当前用户了 获取当前用户的id
            session_start();
            $uid=$_SESSION['id'];
            // //获取当前的时间
            $datime=time();
            $res1=DB::table('guanxiwz')->insertGetId(['uuuid'=>$uid,'wzid'=>$id2,'wztime'=>$datime]);
            if ($res && $res1) {
               echo $npass;
            }else{
                echo 0;
            }
        }
    }
     public function getDepass(Request $request){
        //获取发送过来的id  id1点赞回答问题
        $id1=$request->input('id1');
        if ($id1) {
            $pass=$request->input('pass');
            
            $npass=$pass-1;//点赞数加1
            $res=DB::table('answer')->where('id',$id1)->update(['pass'=>$npass]);//进行修改操作
            
            //删除点赞记录
            $res=DB::table('guanxida')->where('did',$id1)->delete();
            if ($res) {
              echo $npass;
            }
    }
  }
    //ajax取消点赞操作 
    //取消关注问题操作  ajax
     public function getQxgz(Request $request){
        //获取发送过来的id  id为问题的id
        $id=$request->input('id');
        //删除
        $res=DB::table('guanxiwt')->where('wid',$id)->delete();
        if ($res) {
          echo $res;
        }
      }
    //关注问题操作  ajax
     public function getGzwt(Request $request){
        //获取发送过来的id  id为问题的id
        $id=$request->input('id');
        //谁点的赞当然是当前用户了 获取当前用户的id
        session_start();
        $uid=$_SESSION['id'];
        $datime=time();
        $res=DB::table('guanxiwt')->insertGetId(['uuid'=>$uid,'wid'=>$id,'wtime'=>$datime]);
        if ($res) {
          echo $res;
        }
        //往关注问题关系表中插入数据
    //     if ($id) {
    //         $pass=$request->input('pass');
            
    //         $npass=$pass+1;//点赞数加1
    //         $res=DB::table('answer')->where('id',$id1)->update(['pass'=>$npass]);//进行修改操作
    //         //用户执行了点赞答案操作,往点赞答案表中插入数据
    //         //谁点的赞当然是当前用户了 获取当前用户的id
    //         session_start();
    //         $uid=$_SESSION['id'];
    //         // //获取当前的时间
    //         $datime=time();
    //         $res1=DB::table('guanxida')->insertGetId(['uid'=>$uid,'did'=>$id1,'datime'=>$datime]);
    //         if ($res && $res1) {
    //            echo $npass;
    //         }else{
    //             echo 0;
    //         }
    // }
  }
    //ajax操作查询评论
    public function getSelectpl(Request $request){
        //接受数据
        $daid=$request->input('daid');
        // echo $daid;
        //查询表中的数据
        $data=DB::table('comment')
            ->join('userDetails', 'comment.uid', '=', 'userDetails.uid')
            ->select('userDetails.nickName', 'comment.contentp', 'comment.time')
            ->where('aid',$daid)
            ->get();
           
        echo json_encode($data);
    }
    //ajax操作查询评论文章的评论
    public function getSelectp2(Request $request){
        //接受数据
        $daid=$request->input('daid');
        // echo $daid;
        //查询表中的数据
        $data=DB::table('comment')
            ->join('userDetails', 'comment.uid', '=', 'userDetails.uid')
            ->select('userDetails.nickName', 'comment.contentp', 'comment.time')
            ->where('a2id',$daid)
            ->get();
           
        echo json_encode($data);
    }
    //ajax操作评论
    public function getInsertpl(Request $request){
        //接受数据
        $daid=$request->input('daid');
        $val=$request->input('val');
        //获取当前时间
        $time=time();
        //获取当前用户昵称
        session_start();
        $nickName=$_SESSION['nname'];
        $uid=$_SESSION['id'];

        // echo $daid;
        //把数据插入表中
        $data=DB::table('comment')->insertGetId(['uid'=>$uid,'aid'=>$daid,'contentp'=>$val,'time'=>$time]);
           //返回昵称和时间
        // echo $nickName,$time;
        //合并两个变量
        $newdata=$nickName.','.$time;
        echo $newdata;
    }
    public function getInsertpl1(Request $request){
        //接受数据
        $daid=$request->input('daid');
        $val=$request->input('val');
        //获取当前时间
        $time=time();
        //获取当前用户昵称
        session_start();
        $nickName=$_SESSION['nname'];
        $uid=$_SESSION['id'];

        // echo $daid;
        //把数据插入表中
        $data=DB::table('comment')->insertGetId(['uid'=>$uid,'a2id'=>$daid,'contentp'=>$val,'time'=>$time]);
           //返回昵称和时间
        // echo $nickName,$time;
        //合并两个变量
        $newdata=$nickName.','.$time;
        echo $newdata;
    }

    //ajax验证用户
    public function getZc(Request $request)
    {
         //接收用户名
        $name=$request->input('name');
        //匹配用户名
        $res=DB::table('user')->where('name',$name)->get();
        if ($res) {
           echo 1;//用户名被注册
        }
    }
    //ajax验证手机号
    public function getYzphone(Request $request)
    {
         //接收用户名
        $phone=$request->input('phone');
        //匹配用户名
        $res=DB::table('user')->where('phone',$phone)->get();
        if ($res) {
           echo 1;//手机号被注册
        }else{
            echo 2;
        }
    }
}
